<template>
	<view>
		<qiun-title-bar title="基本饼状图" />
		<view class="charts-box">
			<qiun-data-charts type="pie" :chartData="pie" :opts="opts"/>
		</view>
		<qiun-title-bar title="柱状图滚动条" />
		<!-- 开启滚动条，需要开启拖动功能，即:ontouch="true" ，微信小程序需要开启canvas2d，否则会很卡，开启2d需要指定canvasId -->
		<view class="charts-box">
			<qiun-data-charts type="column" canvasId="scrollcolumnid"
				:opts="{enableScroll:true,xAxis:{scrollShow:true,itemCount:4,disableGrid:true}}" :ontouch="true"
				:canvas2d="true" :chartData="column" />
		</view>
		<qiun-title-bar title="词云图"/>
		<view class="charts-box">
		  <qiun-data-charts type="word" :chartData="word"/>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pie: {},
				opts: {
					legend: {
						fontSize: 12
					}
				},
				column: {},
				word: {}
			}
		},
		onPullDownRefresh() {
			this.getCategories()
			this.getTopBlogs()
			this.getTags()
		},
		onLoad() {
			this.getCategories()
			this.getTopBlogs()
			this.getTags()
		},
		methods: {
			getTags(){
				this.word = {}
				let json = {
					"series": []
				}
				let word = JSON.parse(JSON.stringify(json))
				let url = this.$params.host + this.$params.action_tags
				this.$request.get(url,res => {
					url = this.$params.host + this.$params.action_tags_count
					res.data.forEach(async (t) => {
						let r = await this.$request.requestAsync(url + t.id)
						if(r.data.success && r.data.data > 0){
							let tag = {
								name: t.name,
								textSize: parseInt(r.data.data) * 10 > 25 ? 25 : parseInt(r.data.data) * 10
							}
							word.series.push(tag)
						}
					})
					this.word = word
				},() => {
					uni.stopPullDownRefresh()
				})
			},
			getTopBlogs(){
				this.column = {}
				let json = {
					"categories": [],
					"series": [{
						"name": "阅读量",
						"data": []
					},{
						"name": "评论数",
						"data": []
					}]
				}
				this.column = JSON.parse(JSON.stringify(json))
				let url = this.$params.host + this.$params.action_hot
				this.$request.get(url,res => {
					res.data.forEach(blog => {
						this.column.categories.push(this.$util.ellipsis(blog.title,10))
						this.column.series[0].data.push(blog.readCount)
						this.column.series[1].data.push(blog.comments.length)
					})
				},() => {
					uni.stopPullDownRefresh()
				})
			},
			getCategories() {
				this.pie = {}
				let json = {
					"series": [{
						"data": []
					}]
				}
				this.pie = JSON.parse(JSON.stringify(json))
				let url = this.$params.host + this.$params.action_categories
				this.$request.get(url, res => {
					url = this.$params.host + this.$params.action_category_count
					res.data.forEach(async (c) => {
						let r = await this.$request.requestAsync(url + c.id)
						if (r.data.success && r.data.data > 0) {
							let category = {
								name: c.name,
								value: r.data.data
							}
							this.pie.series[0].data.push(category)
						}
					})
				}, () => {
					uni.stopPullDownRefresh()
				})
			}
		}
	}
</script>

<style>
	.charts-box {
		width: 100%;
		height: 300px;
	}
</style>